package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.User;
import com.example.entity.vo.RegisterVo;
import com.example.mapper.UserMapper;
import com.example.service.UcenterMemberService;
import com.example.utils.JwtUtils;
import com.example.utils.MD5;
import com.example.utils.WdException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UserMapper, User> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录
     *
     * @param member
     * @return jwtToken 用户标识
     */
    @Override
    public String login(User member) {
        //获取登录手机号和密码
        String mobile = member.getPhone();
        String password = member.getPassword();

        //手机好和密码非空判断
        if (StringUtils.isEmpty(member) || StringUtils.isEmpty(password)) {
            throw new WdException(20001, "登陆失败!");
        }

        //判断手机号是否正确
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", mobile);
        User mobileMenber = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (mobileMenber == null) {
            throw new WdException(20001, "手机号不存在！");
        }

        //判断密码 md5加密
        if (!MD5.encrypt(password).equals(mobileMenber.getPassword())) {
            throw new WdException(20001, "密码不正确！");
        }

        //判断用户是否被禁用
        if (mobileMenber.getIsDeleted() == 1) {
            throw new WdException(20001, "用户已被禁用！");
        }

        //登录成功，生成token字符串
        String jwtToken = JwtUtils.getJwtToken(mobileMenber.getUserId(), mobileMenber.getName());

        return jwtToken;
    }

    /**
     * 注册
     *
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获得注册数据
        String mobile = registerVo.getPhone(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码
        String code = registerVo.getCode(); //验证码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname)) {
            throw new WdException(20001, "注册失败!");
        }

        if (StringUtils.isEmpty(code)) {
            throw new WdException(20001, "验证码过期！");
        }

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new WdException(20001, "验证码错误！");
        }

        //判断手机号是否重复，表里有相同手机号则不添加
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new WdException(20001, "该手机号已注册！");
        }

        //数据添加数据库中
        User ucenterMember = new User();
        ucenterMember.setPhone(mobile);
        ucenterMember.setAvatar("https://gulicollege-picture.oss-cn-guangzhou.aliyuncs.com/2023/02/14/4381c2eb79b447c8a01baf8d8557d76901.jpg");
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setName(nickname);
        ucenterMember.setIsDeleted(0); //用户不禁用
        baseMapper.insert(ucenterMember);

    }

    /**
     * 根据opendid查询
     *
     * @param openid
     * @return ucenter对象
     */
    @Override
    public User getOpenIdMember(String openid) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        User member = baseMapper.selectOne(wrapper);
        return member;
    }
}
